﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ADDProduct : System.Web.UI.Page
{
    public string fname;
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!IsPostBack)
        {
            string sql = "select * from 分类";
            DataSet ds = new DataSet();
            ds = new Class1().hsggetdata(sql);
            this.DropDownList1.DataSource = ds.Tables[0];
            this.DropDownList1.DataTextField = "分类";
            this.DropDownList1.DataValueField = "ID";
            this.DropDownList1.DataBind();

           
        }
    }



    private void hsgupload()
    {
        if (UploadFile.Value != null)
        {
            string nam = UploadFile.PostedFile.FileName;
            //取得文件名(抱括路径)里最后一个"."的索引
            int i = nam.LastIndexOf(".");
            //取得文件扩展名
            string newext = nam.Substring(i);
            //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
            DateTime now = DateTime.Now;
            string newname = now.ToFileTimeUtc() + UploadFile.PostedFile.ContentLength.ToString();

            //保存文件到你所要的目录,这里是IIS根目录下的uploadfiles目录
            //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
            UploadFile.PostedFile.SaveAs(Server.MapPath("../UpLoad/" + newname + newext));

            //得到这个文件的相关属性:文件名,文件类型,文件大小
            //string fname;
            fname = newname + newext;
            //Response.Write("<script>javascript:alert('"+fname+"');</script>");
            //fenc.Text=UploadFile.PostedFile.ContentType ;
            //fsize.Text=UploadFile.PostedFile.ContentLength.ToString();
            //Image1.ImageUrl = "http://localhost/web/news/uploadfiles/"+newname+newext;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
            if (UploadFile.Value != null && UploadFile.Value != "")
            {
                hsgupload();
            }
            string sql;
            sql = "insert into 商品信息(商品编号,商品名称,入库日期,分类,价格,图片)values('" + this.TextBox1.Text + "','" + TextBox2.Text + "','" +  this.Text1.Value + "','" + this.DropDownList1.SelectedItem.Text + "','" + TextBox5.Text + "','" + this.fname + "')";
            int result;
            result = new Class1().hsgexucute(sql);
            if (result == 1)
            {
                Response.Write("<script>javascript:alert('添加成功');</script>");
            }
            else
            {
                Response.Write("<script>javascript:alert('系统错误');</script>");
            }
        }
       
       
  

}
